# -*- coding: utf-8 -*-

import scrapy
import re
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from ..base_parser import BasePageParser
from ...utils import ContentProcessor, FileUtils
from .common import update_media_urls, extract_current_page_menu
from fire_control_spider.config import DEBUG
class LiveeducationParser(BasePageParser):
    """SFPE新闻详情页面解析器"""
    
    def can_handle(self, response):
        """判断是否能处理该响应"""
        return response.url == 'https://www.sfpe.org/events-education/liveeducation/virtual-education' or response.meta.get('page_type') == 'sfpe_liveeducation_detail' 


    def parse(self, response):
        page_type = response.meta.get('page_type')
        if page_type == 'sfpe_liveeducation_detail':
            yield from self.parse_liveeducation_detail(response)
        else:
            """解析新闻详情页面"""
            soup = BeautifulSoup(response.text, 'lxml')

            link_list = soup.select("#MainCopy_ContentWrapper ul li a.margin-bottom-small")
            for link in link_list:
                href = link.get('href')
                if not href:
                    continue
                href = "https://www.pathlms.com/sfpe/courses/105216"
                yield scrapy.Request(
                    url=href,
                    callback=self.spider.parse, 
                    meta={
                        'page_type': 'sfpe_liveeducation_detail',
                        'category_url': response.url
                    }
                )
                if DEBUG:
                    break

    def parse_liveeducation_detail(self, response):
        soup = BeautifulSoup(response.text, 'lxml')

        # 创建WebPageItem
        item = self.create_webpage_item(response)
        item['category'] = ['Home', 'Course']
        
        # 提取标题
        title_selectors = [
            'h1.main-heading',
        ]
        
        title = ''
        for selector in title_selectors:
            title_elem = soup.select_one(selector)
            if title_elem:
                title = title_elem.get_text(strip=True)
                break
        
        item['title'] = title
        
        publish_time = ''
        item['publish_time'] = ''
        
        # 提取正文内容
        content_selectors = [
            '#main-container .content-item-container-section',
        ]
        
        main_body = ''
        for selector in content_selectors:
            content_elem = soup.select_one(selector)
            if content_elem:
                # 处理图文交错内容
                main_body, media_urls = ContentProcessor.process_content_with_media(
                    str(content_elem), response.url, 'sfpe'
                )
                item['main_body'] = main_body

                # 更新媒体URLs
                update_media_urls(item, media_urls)
        
        item['main_body'] = main_body
        
        
        yield item
    
    